<template>
  <Modal :mask-closable="false"
      v-model="opened"
      class="modal-content-layout"
      :title="title">
    <Form class="formList" ref="formCustom" :rules="ruleValidate" :model="formItem" label-position="top">

      <FormItem prop="roleId" :label="$t('部门名称')" class="textlab">
        <Select v-model="formItem.deptId"
                clearable
                filterable
                :placeholder="`${$t('common.selectInfo')}`">
          <Option v-for="item in list"
                  :value="item.id"
                  :key="item.id"
                  :label="item.deptName"
          />
        </Select>
      </FormItem>

<!--      <FormItem prop="status" label="状态：" class="textlab">-->
<!--        <RadioGroup v-model="formItem.status">-->
<!--          <Radio :label="0">{{ $t('common.disable') }}</Radio>-->
<!--          <Radio :label="1">启用</Radio>-->
<!--        </RadioGroup>-->
<!--      </FormItem>-->

    </Form>
    <div slot="footer">
      <Button type="default" @click="close">{{ $t('common.Cancle') }}</Button>
      <Button type="primary" :loading="submitLoading" @click="submit">{{ $t('maintenance.Confirmed') }}</Button>
    </div>
  </Modal>
</template>

<script>

export default {
  name: '',
  data() {
    return {
      opened: false,
      title: '',
      formItem: {
        deptId: '',
        status: 1
      },

      list: [],
      submitLoading: false,
      ruleValidate: {
        deptId: [
          {required: true, type: 'number', message: 'role cannot be empty', trigger: 'blur'}
        ],
        // status: [
        //   {required: true, type: 'number', message: 'status cannot be empty', trigger: 'blur'}
        // ],
      }
    }
  },
  mounted() {

  },
  methods: {
    getByName() {
      this.axios({url: "/basic/dept/queryDeptByName", method: "get"}).then(res => {
        if (res.code === 200) {
          this.list = res.data;
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      })
    },
    open(currentNode, submitTo) {
      this.getByName();
      this.submitTo = submitTo;

      if (submitTo === 'add') {
        this.title = this.$t('新增部门')
        this.formItem = {
          parentId: currentNode.id || 0,
          deptId: '',
          status: 1
        }
      } else {
        this.title = this.$t('编辑部门')
        this.formItem = {
          parentId: currentNode.parentId || 0,
          deptId: currentNode.deptId,
          // status: currentNode.status,
          id: currentNode.id
        }
      }

      this.submitLoading = false;
      this.opened = true;
    },
    close() {
      this.opened = false;
    },
    submit() {

      this.$refs['formCustom'].validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          if (this.submitTo === 'add') {
            this.add();
          } else {
            this.edit();
          }
        }
      });
    },
    edit() {
      this.axios({url: "/basic/dept/updateDeptSuper", data: this.formItem,method:"post"}).then(res => {
        if (res.code === 200) {
          this.close();
          this.$Message.success(res.msg);
          this.$emit('save');
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      }).finally(() => {
        this.submitLoading = false;
      })
    },
    add() {
      this.axios({url: "/basic/dept/insertDeptSuper", data: this.formItem,method:"post"}).then(res => {
        if (res.code === 200) {
          this.close();
          this.$Message.success(res.msg);
          this.$emit('save');
        } else {
          this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
        }
      }).finally(() => {
        this.submitLoading = false;
      })
    }
  },
}
</script>
